package com.youku.adSDK.view.components
{
   import flash.display.Sprite;
   import flash.display.MovieClip;
   import flash.utils.Timer;
   import flash.display.Shape;
   import flash.events.TimerEvent;
   import com.youku.utils.AssetUtil;
   import flash.events.Event;
   import com.youku.adSDK.Utils.AdOpenWindow;
   import com.youku.adSDK.view.events.ADHolderEvent;
   
   public class ADHolder extends Sprite
   {
      
      public function ADHolder(param1:Number = 30) {
         super();
         this._num = param1;
      }
      
      private var _width:Number;
      
      private var _height:Number;
      
      private var _barH:Number;
      
      private var _content:Sprite;
      
      private var _info:Sprite;
      
      private var _adTime:MovieClip;
      
      private var _timer:Timer;
      
      private var _num:Number = 30;
      
      private var _back:Shape;
      
      private var _starttime:Number = 0;
      
      public function init(param1:Number, param2:Number, param3:Number) : void {
         this.createItems();
         this.addEvents();
         this.layoutItems(param1,param2,param3);
      }
      
      public function show() : void {
         this._timer.reset();
         this._timer.start();
         this._starttime = new Date().time;
      }
      
      public function stop() : void {
         if(this._timer)
         {
            this._timer.stop();
            this._timer.removeEventListener(TimerEvent.TIMER,this.onTimer);
         }
      }
      
      private function createItems() : void {
         this._back = new Shape();
         this._content = new Sprite();
         this._info = AssetUtil.getAsset("AD_holdInfo") as Sprite;
         this._width = this._info.width;
         this._height = this._info.height;
         this._adTime = AssetUtil.getAsset("AD_holdTime") as MovieClip;
         this._adTime.theTime.text = this._num;
         this._adTime.stop();
         this.addChild(this._back);
         this.addChild(this._content);
         this._content.addChild(this._info);
         this.addChild(this._adTime);
      }
      
      private function addEvents() : void {
         if(this._timer)
         {
            this._timer.stop();
            this._timer.removeEventListener(TimerEvent.TIMER,this.onTimer);
         }
         this._timer = new Timer(1000);
         this._timer.addEventListener(TimerEvent.TIMER,this.onTimer);
         this._info.addEventListener("onPressLink",this.onLinkClick);
         this._info.addEventListener("onPressLink1",this.onLinkClick1);
      }
      
      private function onLinkClick(param1:Event) : void {
         AdOpenWindow.openURL("http://www.youku.com/help/view/fid/2#q8");
      }
      
      private function onLinkClick1(param1:Event) : void {
         AdOpenWindow.openURL("http://www.youku.com/service/feed/subtype_33/");
      }
      
      private function onTimer(param1:TimerEvent) : void {
         var _loc2_:Number = new Date().time;
         var _loc3_:int = (_loc2_ - this._starttime) / 1000;
         if(_loc3_ > this._num)
         {
            this.dispatchEvent(new ADHolderEvent(ADHolderEvent.FINISHED));
            this._timer.stop();
            this._timer.removeEventListener(TimerEvent.TIMER,this.onTimer);
         }
         this._adTime.theTime.text = this._num - _loc3_;
      }
      
      public function layoutItems(param1:Number, param2:Number, param3:Number) : void {
         var _loc4_:* = NaN;
         if(stage)
         {
            _loc4_ = 1;
            if(this._width / this._height > param1 / param2)
            {
               if(this._width > param1)
               {
                  _loc4_ = param1 / this._width;
               }
            }
            else if(this._height > param2)
            {
               _loc4_ = param2 / this._height;
            }
            
            this._content.scaleX = this._content.scaleY = _loc4_;
            this._content.x = int((param1 - this._width * _loc4_) / 2);
            this._content.y = int((param2 - param3 - this._height * _loc4_) / 2);
            this._adTime.x = param1 - this._adTime.width - 5;
            this._adTime.y = 5;
            this._back.graphics.clear();
            this._back.graphics.beginFill(0);
            this._back.graphics.drawRect(0,0,param1,param2);
            this._back.graphics.endFill();
         }
      }
      
      public function get time() : Number {
         return this._num;
      }
      
      public function set time(param1:Number) : void {
         this._num = param1;
      }
      
      public function getCurrentTime() : Number {
         return Number(this._adTime.theTime.text);
      }
   }
}
